<?php

require_once "tfsbox_abstract_view.class.php";

class TFsBOXShoutsView extends TFsBOXAbstractShoutView
	{
		function __construct()
		{
		}

		protected function renderPagination()
			{
				if ($count = TFsBOX::$Database->numOfShouts()) // Avoids zero
				{
					echo "<div id=\"tfsbox_pagination\">\n";

					$limit = (int) TFsBOX::$Config->value("shouts_per_page", 10);
					$total = $count / $limit;
					$cur_page = (int) TFsBOX::$Session->currentShoutPage();
					if (!$cur_page)
						$cur_page = 1;
					$page_prefix = $_SERVER['SCRIPT_NAME'] ."/tfsbox/page/";

					if ($cur_page > 1)
						echo "<a href=\"". $page_prefix . ($cur_page - 1) ."\">&lt;</a>\n";

					for ($int = 0; $int < $total; $int++)
					{
						if ($cur_page != ($int + 1))
							echo "<a href=\"". $page_prefix . ($int + 1) ."\">". ($int + 1) ."</a>\n";
						else
							echo "<b>". ($int + 1) ."</b>\n";
					}

					if ($cur_page < $total)
						echo "<a href=\"". $page_prefix . ($cur_page + 1) ."\">&gt;</a>\n";

					echo "</div>\n";
				}
			}

		static function parseSmilies($shout)
			{
				foreach (TFsBOX::$Database->smileyBbcodes() as $key => $value)
				{
					if ($key = TFsBOX::$Database->smiley($key))
						$shout = preg_replace("#". $value ."#i",
											  "<img src=\"". TFsBOXSession::urlRoot() ."/". TFsBOX::$Config->value("smiley_pack", "images/smileys/trillian") ."/". $key ."\" style=\"vertical-align: middle;\" alt=\"$key\"/>",
											  $shout);
				}

				return $shout;
			}

		function renderShoutForm()
			{
				echo "<form id=\"tfsbox_shoutform\" action=\"". $_SERVER['SCRIPT_NAME'] ."\" method=\"post\" onsubmit=\"tfsbox_add_shout('". $_SERVER['SCRIPT_NAME'].'/tfsbox/shouts' ."'); return false;\">\n";
				echo "<table cellspacing=\"3\" cellpadding=\"3\" style=\"border: 0; width: 100%;\">\n";
				echo "<tr>\n";
				echo "<td><b style=\"display: block;\">Name:</b></td>\n";

				if (!TFsBOX::$Session->hasUsername())
					echo "<td id=\"tfsbox_name_field\"><input name=\"tfsbox_name\" class=\"tfsbox_textfield\" title=\"Only letters, numbers and underscores allowed for name. Name must start with a letter.\" type=\"text\"/></td>\n";
				else
					echo "<td id=\"tfsbox_name_field\"><b>". TFsBOX::$Session->username() ."</b><input type=\"hidden\" name=\"tfsbox_name\" value=\"". TFsBOX::$Session->username() ."\"/></td>\n";

				echo "<td valign=\"top\" rowspan=\"4\" style=\"text-align: center; width: 36px;\">\n";
				echo "<div class=\"button\" title=\"HTML Tags\">A\n";

				$this->renderTextOptions();

				echo "</div>\n";
				echo "<div class=\"button\" style=\"line-height: 0;\" title=\"Smilies\">";
				echo "<img src=\"". TFsBOXSession::urlRoot() ."/". TFsBOX::$Config->value("smiley_pack", "images/smileys/trillian") ."/". TFsBOX::$Database->smiley("smile") ."\" alt=\"\"/>\n";

				$this->renderSmilies();

				echo "</div>\n";
				echo "</td>\n";
				echo "</tr>\n";
				echo "<tr>\n";
				echo "<td><b style=\"display: block;\">Site:</b></td>\n";

				if (!TFsBOX::$Session->hasUserSite())
				{
					if (!TFsBOX::$Session->loggedIn())
						echo "<td id=\"tfsbox_site_field\"><input name=\"tfsbox_site\" class=\"tfsbox_textfield\" title=\"URL must start with http:// and end with / and cannot contain port numbers or sub-directories.\" type=\"text\" value=\"http://\"/></td>";
					else
						echo "<td id=\"tfsbox_site_field\"><b>". TFsBOX::$Session->userSite() ."</b><input type=\"hidden\" name=\"tfsbox_site\" value=\"". TFsBOX::$Session->userSite() ."\"/></td>\n";
				}

				else
					echo "<td id=\"tfsbox_site_field\"><b>". TFsBOX::$Session->userSite() ."</b><input type=\"hidden\" name=\"tfsbox_site\" value=\"". TFsBOX::$Session->userSite() ."\"/></td>\n";

				echo "</tr>\n";
				echo "<tr>\n";
				echo "<td><b style=\"display: block;\">Shout:</b></td>\n";
				echo "<td><input name=\"tfsbox_shout\" class=\"tfsbox_textfield\" type=\"text\"/></td>";
				echo "</tr>\n";
				echo "<tr><td></td>";
				echo "<td style=\"text-align: center;\">";
				echo "<input type=\"reset\" value=\"Clear\"/>\n";
				echo "<input type=\"submit\" value=\"Shout\"/>";
				echo "</td></tr>\n";
				echo "</table>\n";
				echo "</form>\n";
			}

		static function renderShout($shout)
			{
				$options = false;
				$sep = ":";
				$r_class = ($shout->report
						 && TFsBOX::$Session->userRanking() != TFsBOXSession::REGULAR)
						 ? " class=\"tfsbox_reported\"" : "";

				$me = "";
				if (strlen($shout->shout) > 3)
				{
					$me = $shout->shout[0] .
						  $shout->shout[1] .
						  $shout->shout[2] .
						  $shout->shout[3];
				}

				if ($me == "/me ")
				{
					$sep = "";
					$shout->shout = str_replace("/me ", "", $shout->shout);
				}

				if (TFsBOX::$Database->isUrlBanned($shout->site))
					$shout->site = TFsBOX_SITE;

				echo "\t<div$r_class";
				if ($me == "/me ")
					echo " style=\"font-weight: bold;\"";
				echo ">\n";
				if ($me == "/me ")
					echo "* ";
				echo "\t<b><a rel=\"nofollow\" href=\"". $shout->site ."\">". $shout->name ."</a>$sep</b>\n";
				echo "\t". self::parseSmilies($shout->shout) ."\n";
				echo "\t<div class=\"tfsbox_shout_time\">". date('M d, Y / g:i:s A T', $shout->date) ."</div>\n";
				echo "\t\t<div class=\"tfsbox_shout_options\">\n";

				// Flatfile
				if (!TFsBOX::$Database->databaseMode()
				 && strtolower($shout->name) == strtolower(TFsBOX::USERNAME))
					$shout->ranking = TFsBOXSession::ADMIN;

				// Banned users need not interact with the system
				if (!TFsBOX::$Database->isIpBanned(TFsBOX::$Session->ip()))
				{
					if (TFsBOX::$Session->userRanking() != TFsBOXSession::REGULAR)
					{
						$options = true;
						$cp = (defined("TFsBOX_CONTROL_PANEL")) ? "/cpanel" : "";

						if ($shout->ranking == TFsBOXSession::REGULAR
						 || TFsBOX::$Session->userRanking() == TFsBOXSession::ADMIN)
						{
							if ($shout->ranking != TFsBOXSession::ADMIN
							 && !TFsBOX::$Database->isIpBanned($shout->ip))
								TFsBOXAbstractView::renderButton("Ban IP", $_SERVER['SCRIPT_NAME']."/tfsbox$cp/ban/ip/".ip2long($shout->ip), TFsBOXSession::urlRoot()."/images/icons/ban-ip.png", NULL, 2);

							if (preg_match(TFsBOX::SiteRegex, $shout->site)
							 && $shout->site != TFsBOX_SITE
							 && !TFsBOX::$Database->isUrlBanned($shout->site))
								TFsBOXAbstractView::renderButton("Ban URL", $_SERVER['SCRIPT_NAME']."/tfsbox$cp/ban/url?".rawurlencode($shout->site), TFsBOXSession::urlRoot() ."/images/icons/ban-url.png", NULL, 2);
						}

						TFsBOXAbstractView::renderButton("Edit Shout", "javascript: edit_shout(".$shout->id.", '".$_SERVER['SCRIPT_NAME']."/tfsbox$cp/shouts/edit');", TFsBOXSession::urlRoot()."/images/icons/document-edit.png", NULL, 2);
						TFsBOXAbstractView::renderButton("Delete Shout", $_SERVER['SCRIPT_NAME']."/tfsbox$cp/shouts/delete/".$shout->id, TFsBOXSession::urlRoot() ."/images/icons/edit-delete.png", NULL, 2);

						if ($shout->report)
							TFsBOXAbstractView::renderButton("Clear Report", $_SERVER['SCRIPT_NAME']."/tfsbox$cp/reports/clear/".$shout->id, TFsBOXSession::urlRoot() ."/images/icons/clear-history.png", NULL, 2);
					}

					else if (!$shout->report
						  && $shout->ranking == TFsBOXSession::REGULAR
						  && $shout->name != TFsBOX::$Session->username())
					{
						$options = true;
						TFsBOXAbstractView::renderButton("Report Shout", $_SERVER['SCRIPT_NAME']."/tfsbox/report/".$shout->id, TFsBOXSession::urlRoot() ."/images/icons/operator.png", NULL, 2);
					}

					if (TFsBOX::$Database->databaseMode()
					 && TFsBOX::$Session->loggedIn()
					 && $shout->name != TFsBOX::$Session->username()
					 && !in_array($shout->name, TFsBOX::$Session->userSubscriptions())
					 && !defined("TFsBOX_CONTROL_PANEL"))
					{
						$options = true;
						TFsBOXAbstractView::renderButton("Subcribe to ".$shout->name, $_SERVER['SCRIPT_NAME']."/tfsbox$cp/subscribe/".$shout->name, TFsBOXSession::urlRoot() ."/images/icons/subscribe.png", NULL, 2);
					}
				}

				if (!$options)
					echo "<span class=\"tfsbox_caption\">There are no options available for this shout.</span>\n";

				echo "\t\t</div>\n";
				echo "\t</div>\n";
			}

		function renderShouts($show_header = true, $show_pagination = true, $show_issues = true)
			{
				if (TFsBOX::$Session->hasErrors()
				 && TFsBOX::pathInfo() == "/tfsbox/shouts"
				 && $show_issues)
				{
					echo "<errors>\n";
					foreach (TFsBOX::$Session->errors() as $error)
					{
						echo "<error>$error</error>\n";
					}
					echo "</errors>\n";
				}

				else
				{
					if (TFsBOX::$Session->hasNotices()
					 && TFsBOX::pathInfo() == "/tfsbox/shouts"
					 && $show_issues)
					{
						echo "<notices>\n";
						foreach (TFsBOX::$Session->notices() as $notice)
						{
							echo "<notice>$notice</notice>\n";
						}
						echo "</notices>\n";
					}

					if (TFsBOX::pathInfo() == "/tfsbox/shouts")
						echo "<div id=\"tfsbox_content\">";

					if (empty($_SERVER['QUERY_STRING']) && $show_pagination)
						$this->renderPagination();

					echo "<div id=\"tfsbox_shouts\">\n";

					if ($show_header)
						echo "<h2>Shouts</h2>\n";

					$arr = TFsBOX::$Database->shouts();
					if (!TFsBOX::$Database->databaseMode())
					{
						$arr = array_splice(array_reverse($arr),
											(TFsBOX::$Session->currentShoutPage()-1) * (int) TFsBOX::$Config->value("shouts_per_page", 10),
											(int) TFsBOX::$Config->value("shouts_per_page", 10));
					}

					if (empty($arr))
					{
						if (!isset($_SERVER['QUERY_STRING']))
						{
							$li = (!TFsBOX::$Database->allowedToShout()) ? "<br/>\n(You must be logged in to shout.)" : "";
							echo "<div style=\"text-align: center;\">No one has shouted. Be the first to shout!$li</div>\n";
						}

						else echo "<div style=\"text-align: center;\">No shouts to display.</div>\n";
					}

					else
					{
						$class = get_class(TFsBOX::$View);
						foreach ($arr as $shout)
							call_user_func(array($class, 'renderShout'), $shout);
					}

					echo "</div>\n";

					if (TFsBOX::pathInfo() == "/tfsbox/shouts")
						echo "</div>\n";
				}
			}

		protected function renderSmilies()
			{
				echo "<div id=\"tfsbox_smilies\" class=\"tfsbox_menu\">\n";
				echo "<img src=\"". TFsBOXSession::urlRoot() ."/images/icons/detach.png\" onclick=\"detach(this, 'tfsbox_smilies')\" align=\"right\" style=\"cursor: pointer;\" alt=\"Detach\"/>\n";
				echo "<img src=\"". TFsBOXSession::urlRoot() ."/images/icons/attach.png\" onclick=\"attach(this, 'tfsbox_smilies')\" align=\"right\" style=\"cursor: pointer; display: none;\" alt=\"Attach\"/>\n";
				echo "<h3>Smilies</h3>\n";
				echo "<div style=\"overflow: auto; height: 200px;\">\n";

				foreach (TFsBOX::$Database->smilies() as $key => $value)
				{
					echo "<div>";
					$bbcode = preg_split("#(?<!\\\)\|#", TFsBOX::$Database->smileyBbcodes($key), -1, PREG_SPLIT_NO_EMPTY);
					$bbcode = array_map("stripslashes", $bbcode);
					echo "<img onclick=\"tfsbox_insert_smiley(' ". $bbcode[0] ." ')\" style=\"cursor: pointer;\" src=\"". TFsBOXSession::urlRoot() ."/". TFsBOX::$Config->value("smiley_pack", "images/smileys/trillian") ."/$value\" alt=\"$key\"/> ";
					echo "<tt>". implode("</tt> or <tt>", $bbcode) ."</tt>";
					echo "</div>\n";
				}

				echo "</div>\n";
				echo "</div>\n";
			}

		protected function renderTextOptions()
			{
				$h = "h4";
				echo "<div id=\"tfsbox_text_options\" class=\"tfsbox_menu\">\n";
				echo "<img src=\"". TFsBOXSession::urlRoot() ."/images/icons/detach.png\" onclick=\"detach(this, 'tfsbox_text_options')\" align=\"right\" style=\"cursor: pointer;\" alt=\"Detach\"/>\n";
				echo "<img src=\"". TFsBOXSession::urlRoot() ."/images/icons/attach.png\" onclick=\"attach(this, 'tfsbox_text_options')\" align=\"right\" style=\"cursor: pointer; display: none;\" alt=\"Attach\"/>\n";
				echo "<h3>HTML Tags</h3>\n";
				echo "<div style=\"overflow: auto; height: 200px;\">\n";
				echo "<div><$h>Link:</$h> &lt;a href=\"http://www.example.com/\"&gt;Link&lt;/a&gt;<br/>\n<a href=\"http://www.example.com/\">Link</a></div>\n";
				echo "<div><$h>Bold:</$h> &lt;b&gt;bold&lt;/b&gt;<br/>\n<b>bold</b></div>\n";
				echo "<div><$h>Italics:</$h> &lt;i&gt;italic&lt;/i&gt;<br/>\n<i>italic</i></div>\n";
				echo "<div><$h>Underline:</$h> &lt;u&gt;underline&lt;/u&gt;<br/>\n<u>underline</u></div>\n";
				echo "<div><$h>Strike-through:</$h> &lt;s&gt;strike&lt;/s&gt;<br/>\n<s>strike</s></div>";
				echo "<div><$h>Subscript:</$h> text&lt;sub&gt;sub&lt;/sub&gt;<br/>\ntext<sub>sub</sub></div>";
				echo "<div><$h>Superscript:</$h> text&lt;sub&gt;sup&lt;/sup&gt;<br/>\ntext<sup>sup</sup></div>";
				echo "<div><$h>Highlight:</$h> &lt;span style=\"background-color: yellow;\"&gt;text&lt;/span&gt;<br/>\n<span style=\"background-color: yellow;\">text</span></div>\n";
				echo "<div><$h>Font family:</$h> &lt;span style=\"font-family: 'Times New Roman';\"&gt;text&lt;/span&gt;<br/>\n<span style=\"font-family: 'Times New Roman';\">text</span></div>\n";
				echo "<div><$h>Font color:</$h> &lt;span style=\"color: blue;\"&gt;text&lt;/span&gt;<br/>\n<span style=\"color: blue;\">text</span></div>\n";

				if (TFsBOX::$Config->value("allow_images", "no") == "no")
					echo "<div><$h>Images are currently disabled.</$h></div>";
				else
				{
					echo "<div><$h>Images:</$h> &lt;img src=\"". TFsBOXSession::urlRoot() ."/". TFsBOX::$Config->value("smiley_pack", "images/smileys/trillian") ."/". TFsBOX::$Database->smiley("smile") ."\" alt=\"\"/&gt;<br/>\n";
					echo "<img src=\"". TFsBOXSession::urlRoot() ."/". TFsBOX::$Config->value("smiley_pack", "images/smileys/trillian") ."/". TFsBOX::$Database->smiley("smile") ."\" alt=\"\"/></div>";
				}

				echo "</div>\n";
				echo "</div>\n";
			}
	}

?>
